package com.uu.safeguard.service.impl;


import com.uu.safeguard.service.PlatformService;
import com.uu.safeguard.utils.DbUtil;
import com.uu.safeguard.utils.JedisUtil;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.Map;


@Service("PlatformService")
public class PlatformServiceImpl implements PlatformService {
    public Map detail(String sn){
        //判断缓存是否存在
        Jedis jedis = JedisUtil.getJedis();
        jedis.select(2);
        //查询缓存中是否存在
        String redisKey = "platform_" + sn;
        if(jedis.exists(redisKey)){
            String platformStr = jedis.get(redisKey);
            Map platform = JSONObject.fromObject(platformStr);
            JedisUtil.close(jedis);
            return platform;
        }

        DbUtil db = new DbUtil();
        Map platform = db.table("platform").where(new ArrayList() {{
            add(new ArrayList() {{
                add("sn");
                add("=");
                add(sn);
            }});
        }}).find("*");
        JSONObject jsonObject = JSONObject.fromObject(platform);
        jedis.set(redisKey,jsonObject.toString());
        JedisUtil.close(jedis);
        return platform;
    }
}
